#!/usr/bin/env python

import pdb
import urllib2
import sys
from xml.dom import minidom, Node

site_url = "https://dl-ssl.google.com/android/repository/"
fileName = "repository-10.xml"
url = site_url + fileName

download_url_prefix = "https://dl-ssl.google.com/android/repository/"

namespace2 = "{http://schemas.android.com/sdk/android/repository/10}"
namespace = "sdk:"
headers = {'User-Agent':'Mozila/5.0(Windows;U;Windows NT 6.1;en-Us;rv:1.9.1.6)Gecko/20091201 Firefox3.5.6'}

class Archive(object):
    
    __slots__ = ('_size', '_checksum', '_checksum_type', '_url', '_host_os')

    def __init__(self, archivenode):
        self._size = get_nodevalue(get_xmlnode(archivenode, namespace+"size")[0])

        checksumnode = get_xmlnode(archivenode, namespace+"checksum")[0]
        self._checksum = get_nodevalue(checksumnode)
        if get_attrvalue(checksumnode, "type"):
            self._checksum_type = get_attrvalue(checksumnode, "type")

        self._url = get_nodevalue(get_xmlnode(archivenode, namespace+"url")[0])
        if len(get_xmlnode(archivenode, namespace+"host_os")):
            self._host_os = get_nodevalue(get_xmlnode(archivenode, namespace+"host_os")[0])
    
    def __str__(self):
        return "Archive object : size : %s, checksum : %s, checksum_type : %s, url : %s" % (self._size, self._checksum, self._checksum_type, self._url)

class Platform(object):
    
    __slots__ = ('_version', '_api_level', '_revision', '_desc', '_obsolete', '_archives', '_layoutlib', '_license')

    def __init__(self):
        self._archives = []

    def __getattr__(self, value):
        pass

    def __str__(self):
        return '''Platform object (version:=%s, api-level=%s, revision=%s, desc=%s, obsolete=%s, archives=%s, layoutlib=%s, license=%s)
        ...''' % (self._version, self._api_level, self._revision, self._desc, self._obsolete, self._archives, self._layoutlib, self._license)
    
    @property
    def desc(self):
        return self._desc + self._revision + " Api-level : " + self._api_level

    @property
    def version(self):
        return self._version

    @version.setter
    def version(self, version):
        self._version = version
    
    @property
    def api_level(self):
        return self._api_level

    @api_level.setter
    def api_level(self, level):
        self._api_level = level

    @property
    def revision(self):
        return self._revision

    @revision.setter
    def revision(self, revision):
        self._revision = revision

    @property
    def description(self):
        return self._desc
    
    @description.setter
    def description(self, desc):
        self._desc = desc

    @property
    def obsolete(self):
        return self._obsolete

    @obsolete.setter
    def obsolete(self, obsolete):
        self._obsolete
    
    @property
    def archives(self):
        return self._archives

    @archives.setter
    def archives(self, childnode):
        for archivenode in childnode.childNodes:
            if archivenode.nodeType == Node.ELEMENT_NODE:
                 self._archives.append(Archive(archivenode))

    @property
    def layoutlib(self):
        return self._layoutlib
    
    @layoutlib.setter
    def layoutlib(self, childnode):
        self._layoutlib = get_nodevalue(get_xmlnode(childnode, namespace+"api")[0])
                
    @property
    def license(self):
        return self._license

    @license.setter
    def license(self, license):
        self._license = license

class Sample(object):
        
    __slots__ = ('_api_level','_codename', '_revision', '_desc', '_desc_url', '_archives', '_license')

    def __init__(self):
        self._archives = []

    def __getattr__(self, value):
        pass

    def __str__(self):
        return '''Sample object (api-level=%s, codename=%s, revision=%s, desc=%s, desc_url=%s, archives=%s, license=%s)
        ...''' % (self._api_level, self._codename, self._revision, self._desc, self._desc_url, self._archives, self._license)

    @property
    def api_level(self):
        return self._api_level

    @api_level.setter
    def api_level(self, level):
        self._api_level = level

    @property
    def codename(self):
        return self._codename

    @codename.setter
    def codename(self, codename):
        self._codename = codename

    @property
    def revision(self):
        return self._revision

    @revision.setter
    def revision(self, revision):
        self._revision = revision

    @property
    def archives(self):
        return self._archives

    @archives.setter
    def archives(self, childnode):
        for archivenode in childnode.childNodes:
            if archivenode.nodeType == Node.ELEMENT_NODE:
                 self._archives.append(Archive(archivenode))

    @property
    def description(self):
        return self._desc

    @description.setter
    def description(self, description):
        self._desc = description
    
    @property
    def descriptionUrl(self):
        return self._desc_url

    @descriptionUrl.setter
    def descriptionUrl(self, descriptionUrl):
        self._desc_url = descriptionUrl

    @property
    def license(self):
        return self._license

    @license.setter
    def license(self, license):
        self._license = license

class PlatformTool(object):
   
    __slots__ = ('_revision', '_archives', '_license')

    def __init__(self):
        self._archives = []

    def __getattr__(self, value):
        pass

    def __str__(self):
        return "PlatformTool Object revision:%s, archives:%s, license:%s" % (self._revision, self._archives, self._license)

    @property
    def revision(self):
        return self._revision

    @revision.setter
    def revision(self, revision):
        for vision in revision.childNodes:
            if vision.nodeType == Node.ELEMENT_NODE:
                if self._revision :
                    self._revision = self._revision +'.'+ get_nodevalue(vision)
                else:
                    self._revision = get_nodevalue(vision)

    @property
    def archives(self):
        return self._archives

    @archives.setter
    def archives(self, childnode):
       for archivenode in childnode.childNodes:
            if archivenode.nodeType == Node.ELEMENT_NODE:
                 self._archives.append(Archive(archivenode))

    @property
    def license(self):
        return self._license

    @license.setter
    def license(self, license):
        self._license = license

class BuildTools(object):
    
    __slots__ = ('_revision', '_archives', '_license')

    def __init__(self):
        self._archives = []

    def __getattr__(self, value):
        pass
    
    def __str__(self):
        return "BuildTools Object revision:%s, archives:%s, license:%s" % (self._revision, self._archives, self._license)

    @property
    def revision(self):
        return self._revision

    @revision.setter
    def revision(self, revision):
        for vision in revision.childNodes:
            if vision.nodeType == Node.ELEMENT_NODE:
                if self._revision :
                    self._revision = self._revision +'.'+ get_nodevalue(vision)
                else:
                    self._revision = get_nodevalue(vision)

    @property
    def archives(self):
        return self._archives

    @archives.setter
    def archives(self, childnode):
       for archivenode in childnode.childNodes:
            if archivenode.nodeType == Node.ELEMENT_NODE:
                self._archives.append(Archive(archivenode))

    @property
    def license(self):
        return self._license

    @license.setter
    def license(self, license):
        self._license = license

class Doc(object):

    __slots__ = ('_revision', '_api_level', '_archives', '_license')

    def __init__(self):
        self._archives = []
    
    def __getattr__(self, value):
        pass

    def __str__(self):
        return "Doc Object revision:%s, api-level:%s, archives:%s, license:%s" % (self._revision, self._api_level, self._archives, self._license)

    @property
    def revision(self):
        return self._revision

    @revision.setter
    def revision(self, revision):
        self._revision = revision

    @property
    def api_level(self):
        return self._api_level

    @api_level.setter
    def api_level(self, level):
        self._api_level = level

    @property
    def archives(self):
        return self._archives

    @archives.setter
    def archives(self, childnode):
       for archivenode in childnode.childNodes:
            if archivenode.nodeType == Node.ELEMENT_NODE:
                self._archives.append(Archive(archivenode))

    @property
    def license(self):
        return self._license

    @license.setter
    def license(self, license):
        self._license = license

class Source(object):
    
    __slots__ = ('_revision', '_api_level', '_archives', '_license')

    def __init__(self):
        self._archives = []

    def __getattr__(self, value):
        pass
    
    def __str__(self):
        return "Source Object revision:%s, api-level:%s, archives:%s, license:%s" % (self._revision, self._api_level, self._archives, self._license)

    @property
    def revision(self):
        return self._revision

    @revision.setter
    def revision(self, revision):
        self._revision = revision
    
    @property
    def api_level(self):
        return self._api_level

    @api_level.setter
    def api_level(self, level):
        self._api_level = level

    @property
    def archives(self):
        return self._archives

    @archives.setter
    def archives(self, childnode):
       for archivenode in childnode.childNodes:
            if archivenode.nodeType == Node.ELEMENT_NODE:
                self._archives.append(Archive(archivenode))

    @property
    def license(self):
        return self._license

    @license.setter
    def license(self, license):
        self._license = license

class Tool(object):
    
    __slots__ = ('_revision', '_min_platform_tool_rev', '_archives', '_license')

    def __init__(self):
        self._archives = []
    
    def __getattr__(self, value):
        pass

    def __str__(self):
        return "Tool Object revision:%s, min_platform_tool_rev:%s, archives:%s, license:%s" % (self._revision, self._min_platform_tool_rev, self._archives, self._license)

    @property
    def revision(self):
        return self._revision

    @revision.setter
    def revision(self, revision):
        for vision in revision.childNodes:
            if vision.nodeType == Node.ELEMENT_NODE:
                if self._revision :
                    self._revision = self._revision +'.'+ get_nodevalue(vision)
                else:
                    self._revision = get_nodevalue(vision)
    
    @property
    def minPlatformToolRev(self):
        return self._min_platform_tool_rev

    @minPlatformToolRev.setter
    def minPlatformToolRev(self, minPlatformToolRev):
        self._min_platform_tool_rev = minPlatformToolRev

    @property
    def archives(self):
        return self._archives

    @archives.setter
    def archives(self, childnode):
      for archivenode in childnode.childNodes:
            if archivenode.nodeType == Node.ELEMENT_NODE:
                self._archives.append(Archive(archivenode))

    @property
    def license(self):
        return self._license

    @license.setter
    def license(self, license):
        self._license = license

platformList = []
def platforms(node):
    child = node

    platform = Platform()
    
    for childnode in child.childNodes:
        if childnode.nodeType == Node.ELEMENT_NODE:
            tagName = childnode.tagName
            text = get_nodevalue(childnode)

            if tagName == namespace+"version":
                platform.version = text
            elif tagName == namespace+"api-level":
                platform.api_level = text
            elif tagName == namespace+"revision":
                platform.revision = text
            elif tagName == namespace+"description":
                platform.description = text
            elif tagName == namespace+"obsolete":
                platform.obsolete = text
            elif tagName == namespace+"archives":
                platform.archives = childnode
            elif tagName == namespace+"layoutlib":
                platform.layoutlib = childnode
            elif tagName == namespace+"uses-license":
                platform.license = text
    platformList.append(platform)

buildToolList = []
def build_tools(node):
    child =  node
    
    buildTool = BuildTools()

    for childnode in child.childNodes:
        if childnode.nodeType == Node.ELEMENT_NODE:
            tagName = childnode.tagName
            text = get_nodevalue(childnode)

            if tagName == namespace+"revision":
                buildTool.revision = childnode
            elif tagName == namespace+"archives":
                buildTool.archives = childnode
            elif tagName == namespace+"uses-license":
                buildTool.license = text
    #pdb.set_trace()
    buildToolList.append(buildTool)

samplesList = []
def samples(node):
    child =  node
    
    sample = Sample()

    for childnode in child.childNodes:
        if childnode.nodeType == Node.ELEMENT_NODE:
            tagName = childnode.tagName
            text = get_nodevalue(childnode)

            if tagName == namespace+"api-level":
                sample.api_level = text
            elif tagName == namespace+"codename":
                sample.codename = text
            elif tagName == namespace+"revision":
                sample.revision = text
            elif tagName == namespace+"description":
                sample.description = text
            elif tagName == namespace+"desc-url":
                sample.descriptionUrl = text
            elif tagName == namespace+"archives":
                sample.archives = childnode
            elif tagName == namespace+"uses-license":
                sample.license = text
    samplesList.append(sample)

sourceList = []
def sources(node):
    child =  node
    
    source = Source()

    for childnode in child.childNodes:
        if childnode.nodeType == Node.ELEMENT_NODE:
            tagName = childnode.tagName
            text = get_nodevalue(childnode)

            if tagName == namespace+"revision":
                source.revision = text
            elif tagName == namespace+"api-level":
                source.api_level = text
            elif tagName == namespace+"archives":
                source.archives = childnode
            elif tagName == namespace+"uses-license":
                source.license = text
    sourceList.append(source)

platformToolList = []
def platform_tools(node):
    child =  node
    
    platformTool = PlatformTool()

    for childnode in child.childNodes:
        if childnode.nodeType == Node.ELEMENT_NODE:
            tagName = childnode.tagName
            text = get_nodevalue(childnode)

            if tagName == namespace+"revision":
                platformTool.revision = childnode
            elif tagName == namespace+"archives":
                platformTool.archives = childnode
            elif tagName == namespace+"uses-license":
                platformTool.license = text
    platformToolList.append(platformTool)

docsList = []
def docs(node):
    child =  node
    
    doc = Doc()

    for childnode in child.childNodes:
        if childnode.nodeType == Node.ELEMENT_NODE:
            tagName = childnode.tagName
            text = get_nodevalue(childnode)

            if tagName == namespace+"revision":
                doc.revision = text
            elif tagName == namespace+"api-level":
                doc.api_level = text
            elif tagName == namespace+"archives":
                doc.archives = childnode
            elif tagName == namespace+"uses-license":
                doc.license = text
    docsList.append(doc)

toolsList = []
def tools(node):
    child =  node
    
    tool = Tool()

    for childnode in child.childNodes:
        if childnode.nodeType == Node.ELEMENT_NODE:
            tagName = childnode.tagName
            text = get_nodevalue(childnode)

            if tagName == namespace+"revision":
                tool.revision = childnode
            elif tagName == namespace+"min-platform-tools-rev":
                tool.minPlatformToolRev = text
            elif tagName == namespace+"archives":
                tool.archives = childnode
            elif tagName == namespace+"uses-license":
                tool.license = text
    toolsList.append(tool)

def get_attrvalue(node, attrname):
    return node.getAttribute(attrname) if node else ''

def get_nodevalue(node, i=0):
    if len(node.childNodes) :
        return node.childNodes[i].nodeValue

def get_xmlnode(node,name):
    return node.getElementsByTagName(name) if node else []

def get_content_from_local():
    dom = minidom.parse("repository.xml")
    root = dom.documentElement
    return root

def make_download_request(url, use_proxy):
    request = urllib.request.ProxyHandler({'http':'http://127.0.0.1:8087','https':'http://127.0.0.1:8087'})

def make_request(url, use_proxy):
    request = urllib2.Request(url, headers=headers)
    if use_proxy :
        print "use_proxy"
        proxy_support = urllib2.ProxyHandler({'http':'http://127.0.0.1:8087','https':'http://127.0.0.1:8087'})
    else:
        print "no proxy"
        proxy_support = urllib2.ProxyHandler({})
    opener = urllib2.build_opener(proxy_support,urllib2.HTTPHandler(1), urllib2.HTTPSHandler(1))
    urllib2.install_opener(opener)
    return urllib2.urlopen(url)

def get_content_from_net():
    content = make_request(url, True).read()
    dom = minidom.parseString(content)
    root = dom.documentElement
    return root

def parse_repository_content(tree):
    
    #pdb.set_trace()

    for node in tree.childNodes:
        if node.nodeType == Node.ELEMENT_NODE:
            if(node.tagName == namespace+"platform"):
                platforms(node)
            elif(node.tagName == namespace+"sample"):
                samples(node)
            elif(node.tagName == namespace+"platform-tool"):
                platform_tools(node)
            elif(node.tagName == namespace+"build-tool"):
                build_tools(node)
            elif(node.tagName == namespace+"doc"):
                docs(node)
            elif(node.tagName == namespace+"source"):
                sources(node)
            elif(node.tagName == namespace+"tool"):
                tools(node)

    platformLen = len(platformList)
    sampleLen = len(samplesList)
    platformToolLen = len(platformToolList)
    buildToolLen = len(buildToolList)
    docLen = len(docsList)
    sourceLen = len(sourceList)
    toolLen = len(toolsList)

    print "we got google android sdk content here : "
    
    print "platformList size is >>> %d " % platformLen
    print "================================================"
    for platform in platformList:
        print platform
    print "================================================"
    
    print "samplesList size is >>> %d " % sampleLen
    print "================================================"
    for sample in samplesList:
        print sample
    print "================================================"

    print "platformToolList size is >>> %d " % platformToolLen
    print "================================================"
    for platformTool in platformToolList:
        print platformTool
    print "================================================"
    
    print "buildToolList size is >>> %d " % buildToolLen
    print "================================================"
    for buildTool in buildToolList:
        print buildTool
    print "================================================"
    
    print "docsList size is >>> %d" % docLen
    print "================================================"
    for doc in docsList:
        print doc
    print "================================================"
    
    print "sourceList size is >>> %d " % sourceLen
    print "================================================"
    for source in sourceList:
        print source
    print "================================================"
    
    print "toolsList size is >>> %d " % toolLen
    print "================================================"
    for tool in toolsList:
        print tool
    print "================================================"
    
    return (platformLen, sampleLen, platformToolLen, buildToolLen, docLen, sourceLen, toolLen)

def report(count, blockSize, totalSize):
    percent = int(count * blockSize * 100 / totalSize)
    print "\r%d% " % percent + ' complete'

def start_download_file(archive):
    print "start download file ...."
    #request = make_request(archive._url, True)
    request = make_request("http://su.bdimg.com/static/superplus/img/logo_white_ee663702.png", False)
    with open("Tmp.png", "wb") as code:
        code.write(request.read())
    print "Download Complete"

def fetch_all_url_from_archive(somelist):
    if somelist in (platformList, samplesList, platformToolList, buildToolList, docsList, sourceList, toolsList) == False :
        raise TypeError("list must be one of platformList, samplesList, platformToolList, buildToolList, docsList, sourceList, toolsList")

    list_length = len(somelist)
    for item in somelist:
        for archive in item.archives:
            get_sdk_desc_and_downloadurl(item, archive, 0, list_length)

    #for platform in platformToolList:
    #    for archive in platform.archives:
    #            get_sdk_desc_and_downloadurl(platform, archive, 0, platformLen)
    
    #for sample in samplesList:
    #    for archive in sample.archives:
    #       get_sdk_desc_and_downloadurl(sample, archive, 0, sampleLen)
    
    #for platformTool in platformToolList:
    #    for archive in platformTool.archives:
    #        get_sdk_desc_and_downloadurl(platformTool, archive, 0, platformToolLen)

    #for buildTool in buildToolList:
    #    for archive in buildTool.archives:
    #       get_sdk_desc_and_downloadurl(buildTool, archive, 0, buildToolLen)

    #for doc in docsList:
    #    for archive in doc.archives:
    #        get_sdk_desc_and_downloadurl(doc, archive, 0, docLen)

    #for source in sourceList:
    #    for archive in source.archives:
    #        get_sdk_desc_and_downloadurl(source, archive, 0, sourceLen)
    
    #for tool in toolsList:
    #    for archive in tool.archives:
    #        get_sdk_desc_and_downloadurl(tool, archive, 0, toolLen)

def fetch_lastest_url_from_archive(somelist):
    if somelist in (platformList, samplesList, platformToolList, buildToolList, docsList, sourceList, toolsList) == False :
        raise TypeError("list must be one of platformList, samplesList, platformToolList, buildToolList, docsList, sourceList, toolsList")

    index = 0
    archiveLen = len(somelist[-1].archives)
    choiceList = []
    print "please choose file to download from up list "
    print "============================================================================="
    for archive in somelist[-1].archives:
        choiceList.append(index)
        get_sdk_desc_and_downloadurl(somelist[-1], archive, index, archiveLen)
        index = index + 1
    print "============================================================================="
    
    if archiveLen > 0:
        #tips = "please choose file to download from up list "
        tips = ''
        choiceTips = "please input file index : "
        downdload_index = make_option_menu(tips, choiceTips,choiceList)
        start_download_file(somelist[-1].archives[downdload_index])

    #if somelist is platformList:
    #    get_sdk_desc_and_downloadurl(platformList[-1], archive, 0, platformLen)
    
    #for archive in samplesList[-1].archives:
    #    get_sdk_desc_and_downloadurl(samplesList[-1], archive, 0, sampleLen)
    
    #for archive in platformToolList[-1].archives:
    #    get_sdk_desc_and_downloadurl(platformToolList[-1], archive, 0, platformToolLen)

    #for archive in buildToolList[-1].archives:
    #    get_sdk_desc_and_downloadurl(buildToolList[-1], archive, 0, buildToolLen)

    #for archive in docsList[-1].archives:
    #    get_sdk_desc_and_downloadurl(docsList[-1], archive, 0, docLen)

    #for archive in sourceList[-1].archives:
    #    get_sdk_desc_and_downloadurl(sourceList[-1], archive, 0, sourceLen)
    
    #for archive in toolsList[-1].archives:
    #    get_sdk_desc_and_downloadurl(toolsList[-1], archive, 0, toolLen)

def get_sdk_desc_and_downloadurl(platform, archive, index, lenth=0):
    if isinstance(archive, Archive) == False : 
        raise TypeError("archive must instance of Archive")
    
    if archive._url.find(download_url_prefix) < 0:
        archive._url = download_url_prefix + archive._url
   
    print "     FileIndex: [ %d ] : Info %s , Size : %dm" % (int(index), platform.desc, int(archive._size)/1024/1024)

platformLen = 0
sampleLen = 0
platformToolLen = 0
buildToolLen = 0
docLen = 0
sourceLen = 0
toolLen = 0

CONTENT_MODE_LOCAL_FILE = 0
CONTENT_MODE_NETWORK = 1

CONTENT_MODE_GET_LASTEST_UPDATE = 0
CONTENT_MODE_GET_ALL_UPDATE = 1

fetched_items = dict({})

def convert_name_2_index(name):
    pass

CONTENT_TYPE_LIST = [0, 1, 2, 3, 4, 5, 6]
def start_parse_content(choice):
    if isinstance(choice, int) == False:
        raise TypeError("Must input integer!")

    tree = None
    if choice == CONTENT_MODE_LOCAL_FILE:
        tree = get_content_from_local()
    elif choice == CONTENT_MODE_NETWORK:
        tree = get_content_from_net()

    platformLen, sampleLen, platformToolLen, buildToolLen, docLen, sourceLen, toolLen = parse_repository_content(tree)

    tips = '''please choice which to downdload:'''
    print "============================================================================="
    if platformLen > 0:
        print "Fetched Platform"
        fetched_items[0] = platformList
        tips = tips + '''   [0] : platform'''
    if sampleLen > 0:
        print "Fetched Samples"
        fetched_items[1] = samplesList
        tips = tips + '''   [1] : Samples'''
    if platformToolLen > 0:
        print "Fetched PlatformTools"
        fetched_items[2] = platformToolList
        tips = tips + '''   [2] : PlatformTools'''
    if buildToolLen > 0:
        print "Fetched BuildTools"
        fetched_items[3] = buildToolList
        tips = tips + '''   [3] : BuildTools'''
    if docLen > 0:
        print "Fetched Docs"
        fetched_items[4] = docsList
        tips = tips + '''   [4] : Docs'''
    if sourceLen > 0:
        print "Fetched Sources"
        fetched_items[5] = sourceList
        tips = tips + '''   [5] : Sources'''
    if toolLen > 0:
        print "Fetched Tools"
        fetched_items[6] = toolsList
        tips = tips + '''   [6] : Tools'''
    print "============================================================================="

    #tips = '''please choice which to downdload:
    #0 : platform
    #1 : Samples
    #2 : PlatformTools
    #3 : BuildTools
    #4 : Docs
    #5 : Sources
    #6 : Tools   
    #'''
    choiceTips = "please input choice : "
    inputv = make_option_menu(tips, choiceTips, CONTENT_TYPE_LIST)
    
    tips2 = "Want only show lastest update ? y /n :"
    choiceTips2 = "please input choice 0/1 :"

    lastest = make_option_menu(tips2, choiceTips2, [CONTENT_MODE_GET_LASTEST_UPDATE, CONTENT_MODE_GET_ALL_UPDATE])
    
    somelist = fetched_items[inputv]
    if lastest == CONTENT_MODE_GET_LASTEST_UPDATE:
        fetch_lastest_url_from_archive(somelist)
    elif lastest == CONTENT_MODE_GET_LASTEST_UPDATE:
        fetch_all_url_from_archive(somelist)

def make_option_menu(tips, choiceTips, choiceList):
    print tips
    try:
        raw_input_string = raw_input(choiceTips)
        if raw_input_string in ['q', 'quit']:
            print "Bye Bye ~~"
            sys.exit()
        else:
            inputv = int(raw_input_string)
            if inputv not in choiceList:
                raise TypeError() 
    except ValueError as e:
        print "please input right choice, thanks!"
        make_option_menu(tips, choiceTips, choiceList)
    except TypeError as e:
        print "please input right choice, thanks!"
        make_option_menu(tips, choiceTips, choiceList)
    return inputv

def start_usr_choice():
    tips = '''please choice work mode:
    =============================================================================
    0 : local file mode(file must be named with file.xml)
    1 : network mode(will get file from network)
    ============================================================================
    '''
    choiceTips = 'input your choice : 0/1 ?'

    inputv = make_option_menu(tips, choiceTips, [CONTENT_MODE_LOCAL_FILE, CONTENT_MODE_NETWORK])
    if inputv != None:
        start_parse_content(inputv)

def main():
    #try:
        start_usr_choice() 
    #except Exception as e:
    #   print e

if __name__ == '__main__':
    main()
